package cn.chint.manage.controller;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chint.manage.dao.UserDao;
import cn.chint.manage.service.UserService;
import cn.chint.po.User;
import cn.chint.util.UtilClass;

@Controller
public class AuthController { 
	@Autowired
	UserService userService;
	@Autowired
	UserDao userDao;
	public Logger log = Logger.getLogger(getClass());
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "h5_auth_login", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String  h5_auth_login(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response)  {
		try{
			InputStream in = request.getInputStream();
			String reqJson = org.apache.commons.io.IOUtils.toString(in, "utf-8"); 
			com.alibaba.fastjson.JSONObject jb = com.alibaba.fastjson.JSONObject.parseObject(reqJson);
			String code = jb.getString("code");
			log.info("code ="+code);
			Map map = new HashMap<String, String>();
			map.put("code", code);
			map.put("status", 0);
			log.info("h5进入授权");
			String appid="wx487b598049f3a162"   ;
			String Secret = "7afd7b083512837aad16295d2de49b22";
			WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
			wxMpInMemoryConfigStorage.setAppId(appid);
			wxMpInMemoryConfigStorage.setSecret(Secret);
			WxMpService wxMpService = new WxMpServiceImpl();
			wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
			WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
			log.info("token的值为：" + wxMpOAuth2AccessToken.getAccessToken());
			WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
			String token = wxMpOAuth2AccessToken.getAccessToken();
			log.info("h5授权结果---------" + wxMpUser.toString());
			String nickname = wxMpUser.getNickname();
			String openId = wxMpUser.getOpenId();
			String union_id = wxMpUser.getUnionId();
			String url =wxMpUser.getHeadImgUrl();
			//boolean subscribe = wxMpUser.getSubscribe();//用户是否关注了当前公众号
			List<User>  ls  = userService.getSel(openId);
			if(ls.size()==0){
				log.info("没有找到openid   添加");
				User user = new User();
				user.setWxOpenId(openId);
				user.setWx_nickname(UtilClass.filterEmojis(nickname));
				user.setWxUnionId(union_id);
				user.setWx_avatar_url(url);
				user.setToken(wxMpOAuth2AccessToken.getAccessToken());
				user.setCreatetime(new Date());
				userService.getIns(user);
			}else{
				log.info("找到openid  更新token 信息");
				userService.updateUserInfo(openId, token);
			}
			com.alibaba.fastjson.JSONObject  jsonObject  = new com.alibaba.fastjson.JSONObject ();
			jsonObject.put("nickname", nickname);
			jsonObject.put("head_url", url);
			jsonObject.put("openid", openId);
			jsonObject.put("code", 0);
			return jsonObject.toJSONString();
		}catch (Throwable e) {
			e.printStackTrace();
			log.info("错误信息"+e.getMessage());
			com.alibaba.fastjson.JSONObject  jsonObject  = new com.alibaba.fastjson.JSONObject ();
			jsonObject.put("code", 1);
			return jsonObject.toJSONString();
		}
	}
}



